﻿local addonName = "ResetNotifier"
local app = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceEvent-3.0", "AceConsole-3.0")

-- 创建或者加载SavedVariables
ResetSv = ResetSv or {}

-- db
local defaults = {
    profile = {
        today = date("%Y_%m_%d", time())
    }
}

-- 重置
local reset_failed = '无法重置'
local reset_success = '已被重置'

-- 注册数据库
-- _G["ResetSv"] = LibStub("AceDB-3.0"):New("ResetSv", defaults, true)
app.db = LibStub("AceDB-3.0"):New("ResetSv", defaults)

-- 启用插件时
function app:OnEnable()
    self:Print("启用成功 ", ResetSv.today)

    -- 从本地存储读取配置
    self.db:RegisterDefaults(defaults)
    self.db.profile = self.db.profile or defaults

    -- 保存数据到本地
    self.db:SaveProfile()

    -- ResetSv = {
    --     today = date("%Y_%m_%d", time())
    -- }

end

-- 首次加载插件时
function app:OnInitialize()
    self:Print("加载成功 ")

    -- 注册事件
    self:RegisterEvent("CHAT_MSG_SYSTEM")

    self:Print('数据库: ', self.db.profile.today)
    
end

-- 聊天信息系统
function app:CHAT_MSG_SYSTEM(event, msg)
    -- self:Print('msg', msg)
    -- msg = '暴风城监狱 已被重置'
    -- 监控副本重置
    if strfind(msg, reset_success) or strfind(msg, reset_failed) then
        -- 重置副本成功
        if strfind(msg, reset_success) then
            SendChatMessage("{rt1}副本 已重置，请进入副本！{rt1}" , "WHISPER", nil, UnitName("player"))
        else
            SendChatMessage("{rt1}副本 重置失败！{rt1}" , "WHISPER", nil, UnitName("player"))
        end
    end

end

